

from rest_framework import serializers
from goods.models import SKU,SKUSpecification,GoodsCategory,SPU,SPUSpecification,SpecificationOption

class OptModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = SpecificationOption
        fields = [
            'id',
            'value' # 选项名
        ]


class SpecModelSerializer(serializers.ModelSerializer):
    spu = serializers.StringRelatedField()
    spu_id = serializers.IntegerField()

    # 关联从表SpecificationOption选项表多个对象数据
    options = OptModelSerializer(many=True)

    class Meta:
        model = SPUSpecification # 规格表
        fields = [
            'id',
            'name',
            'spu',
            'spu_id',

            'options'
        ]



class SPUSimpleModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = SPU
        fields = [
            'id',
            'name'
        ]


class SKUCategoryModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = GoodsCategory
        fields = [
            'id',
            'name'
        ]



class SKUSpecOptModelSerializer(serializers.ModelSerializer):
    # 注意：外间关联主键隐藏字段，必须显示定义类属性，否则不参与反序列化
    spec_id = serializers.IntegerField()
    option_id = serializers.IntegerField()

    class Meta:
        model = SKUSpecification
        fields = [
            'spec_id',
            'option_id'
        ]


class SKUModelSerializer(serializers.ModelSerializer):
    spu = serializers.StringRelatedField()
    spu_id = serializers.IntegerField()
    category = serializers.StringRelatedField()
    category_id = serializers.IntegerField()

    # specs代表的是什么模型类数据，以及一个还是多个？
    # specs记录的是SKUSpecification模型类对象多条数据
    specs = SKUSpecOptModelSerializer(many=True)

    class Meta:
        model = SKU
        # __all__：显示定义字段，和主键隐藏字段
        fields = "__all__"

    def create(self, validated_data):
        # 重写的目的是：关联隐藏字段specs是从表的数据，用于新建从表对象的；
        # 模型类序列化器默认提供的create方法无法完成从表数据新建；
        # 1、剔除specs
        specs = validated_data.pop('specs') # [{spec_id: 4, option_id: 8},{spec_id: 5, option_id: 11}]
        # 2、新建sku对象
        sku = super().create(validated_data) # 有效数据中只有sku本表参数
        # 3、新建中间表数据记录新增的sku拥有的规格和选项
        for temp in specs:
            # temp = {spec_id: 4, option_id: 8}
            temp['sku_id'] = sku.id # {spec_id: 4, option_id: 8, sku_id: 17}
            SKUSpecification.objects.create(**temp)
        return sku

    def update(self, instance, validated_data):
        # 模型类序列化器默认update方法无法完成中间表数据的更新
        # 1、剔除specs
        #  [{spec_id: "1", option_id: 1}, {spec_id: "2", option_id: 3}, {spec_id: "3", option_id: 6}]
        specs = validated_data.pop('specs')
        # 2、更新主表sku数据
        sku = super().update(instance, validated_data)
        # 3、遍历specs更新中间表数据
        # 删除原有的规格选项
        SKUSpecification.objects.filter(sku_id=sku.id).delete()
        # 根据新的specs插入新的规格选项
        for temp in specs:
            # temp = {spec_id: "1", option_id: 1}
            temp['sku_id'] = sku.id # {spec_id: "1", option_id: 1, sku_id: 20}
            SKUSpecification.objects.create(**temp)

        return sku










